/*
 * Project:     My Project Work, Copyright (C) 2011
 * Created at:  31.07.2011
 */
package de.myprojectwork.model;

import de.myprojectwork.common.util.ConfigurationEnum;
import de.myprojectwork.common.util.Logger;
import de.myprojectwork.common.util.MyProjectWorkException;
import de.myprojectwork.model.dto.Configuration;
import java.util.List;

/**
 *
 * @author Chris Holdschuh
 */
public class ConfigurationModel extends AbstractModel {
    
    private static DataManipulationModel model;
    
    public static String getValue(ConfigurationEnum config) throws MyProjectWorkException {
        if (model == null) 
            model = new DataManipulationModelImpl();
        
        if (config != null) {
            String value = null;
            // aus Enum
            value = config.getValue();
            if (value == null) {
            // aus DB
                try {
                    List<Configuration> list = model.get(Table.CONFIGURATION, Table.CONFIGURATION.clazz(), null);
                    if (list != null && !list.isEmpty()) {
                        for(Configuration c : list) {
                            if (c.getKey().equals(config.getKey())) {
                                value = c.getValue();
                                break;
                            }
                        }
                    } 
                    if (value == null) 
                        throw new MyProjectWorkException("Kein Eintrag für Konfiguration " + config + " gefunden.");
                    
                } catch (MyProjectWorkException ex) {
                    Logger.error("Fehler beim Laden der Konfiguration " + config);
                    throw new MyProjectWorkException("Fehler beim Laden der Konfiguration " + config, ex);
                }
            }
            return value;
        } else {
            throw new IllegalArgumentException("Ungültiger Parameter übergeben.");
        }
    }
}
